<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreInfoBlock {
	var $_head;
	var $_content;
	
	var $_style;
	var $_headstyle;
	var $_contentstyle;
	
	var $_headvisible = true;
	var $_contentvisible = true;
	
	//constructor
	function CoreInfoBlock($head = '', $content = '') {
		$this->_head = $head;
		$this->_content = $content;
	}
	
	//
	function setHead($head) {
		$this->_head = $head;
	}
	
	function getHead() {
		return $this->_head;
	}
	
	function setContent($content) {
		$this->_content = $content;
	}
	
	function addContent($content) {
		if( is_array($this->_content) ) {
			$this->_content[] = $content;
		} else {
			$t = $this->_content;
			unset($this->_content);
			$this->_content = array();
			$this->_content[] = $t;
			$this->_content[] = $content;
		}
	}
	
	function getContent() {
		return $this->_content;
	}
	
	function output() {
		return $this->_format();
	}
	
	function display() {
		echo $this->output();
	}
	
	//style
	function setStyle($style) {
		$this->_style = $style;
	}
	
	function getStyle($style) {
		return $this->_style;
	}
	
	function setHeadStyle($style) {
		$this->_headstle = $style;
	}
	
	function getHeadStyle() {
		return $this->_headstyle;
	}
	
	function setContentStyle($style) {
		$this->_contentstyle = $style;
	}
	
	function getContentStyle() {
		return $this->_contentstyle;
	}
	
	//visible
	function hideHead() {
		$this->_headvisible = false;
	}
	
	function showHead() {
		$this->_headvisible = true;
	}
	
	function hideContent() {
		$this->_contentvisible = false;
	}
	
	function showContent() {
		$this->_contentvisible = true;
	}
	
	//
	function _format_header() {
		$html = '';
		
		if( $this->_headvisible == true ) {
			$html .= '<div class="infoBlockHeading' . (isset($this->_headstyle)? ' ' . $this->_headstyle : '') . '">';
			$html .= $this->_head;
			$html .= '</div>' . "\n";
		}
		
		return $html;
	}
	
	function _format_content() {
		$html = '';
		$html .= '<div class="infoBlockContent' . (isset($this->_contentstyle)? ' ' . $this->_contentstyle : '') . '">' . "\n";
		if( is_array($this->_content) ) {
			foreach($this->_content as $item) {
				$item = $this->_format_convert($item);
				$html .= '<div class="infoBlockItem">' . $item . '</div>' . "\n";
			}
		} else {
			$html .= '<div class="infoBlockItem">' . $this->_content . '</div>' . "\n";
		}
		$html .= '</div>' . "\n";
		
		return $html;
	}
	
	function _format_convert($content) {
		if( preg_match_all("/(={2,})(.+?)(={2,})/i", $content, $matches) ) {
			if( is_array($matches['1']) ) {
				for($i=0, $n=sizeof($matches['1']); $i<$n; $i++) {
					$pattern[] = "/" . $matches['0'][$i] . "/";
					$replacement[] = '<h' . strlen($matches['1'][$i]) . '>' . $matches['2'][$i] . '</h' . strlen($matches['1'][$i]) . '>';
				}
			}
			$content = preg_replace($pattern, $replacement, $content);
		}
		
		return $content;
	}
	
	function _format() {
		$html = '';
		$html .= '<div class="infoBlock' . (isset($this->_style)? ' ' . $this->_style : '') . '">' . "\n";
		$html .= $this->_format_header();
		$html .= $this->_format_content();
		$html .= '</div>' . "\n";
		
		return $html;
	}
}
//
?>